###Diplomatic Statements and the Strategic Use of Terrorism in Civil Wars###
###Levy, Dudley, Chen, Siegel###
###Journal of Conflict Resolution###
###R Script 2: Table A1, A2, A3, A4, A5, and Figure A9 in Online Appendix###

rm(list = ls())

#setwd("Replication")

library(dplyr)
library(lme4)
library(texreg)
library(ggplot2)
library(coefplot)
library(stargazer)
library(merTools)
library(ggeffects)
library(ggpubr)
library(gridGraphics)
library(patchwork)
library(egg)
library(sjPlot)
library(ggpubr)
library(estimatr)
library(lubridate)
library(xtable)
library(Hmisc)
library(pastecs)
library(interplot)
library(cem)
library(MatchIt)
source("Code/functions.R")
load( "Data/df.RData")

####Table A1####

#Fixed-effects
mod2.prop <- lm(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                  prev_res_reb_lag3 + prev_res_gov_lag3 +
                  any_reb_int_lag2 + any_gov_int_lag2 +
                  physint + lngdppc + lntpop + rebstrength2 + 
                  coldwar +rebels_count + duration+
                  factor(country) + factor(year),
                data = df)

mod2.propXP <- lm(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                    prev_res_reb_lag3 + prev_res_gov_lag3 +
                    reb_X_d_lag2 + gov_X_d_lag2 +
                    physint + lngdppc + lntpop + rebstrength2 + 
                    coldwar +rebels_count + duration +
                    factor(country) + factor(year),
                  data = df)

mod2.propWP <- lm(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                    prev_res_reb_lag3 + prev_res_gov_lag3 +
                    reb_W_d_lag2 + gov_W_d_lag2 +
                    physint + lngdppc + lntpop + rebstrength2 + 
                    coldwar +rebels_count + duration +
                    factor(country) + factor(year),
                  data = df)

mod2.propEP <- lm(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                    prev_res_reb_lag3 + prev_res_gov_lag3 +
                    reb_E_d_lag2 + gov_E_d_lag2 +
                    physint + lngdppc + lntpop + rebstrength2 + 
                    coldwar +rebels_count + duration +
                    factor(country) + factor(year),
                  data = df)

## mixed-effects
lmermod2.prop <- lmer(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                        prev_res_reb_lag3 + prev_res_gov_lag3 +
                        any_reb_int_lag2 + any_gov_int_lag2 +
                        physint + lngdppc + lntpop + rebstrength2 + 
                        coldwar +rebels_count + duration+
                        (1 | country) +(1 | year),
                      data = df)

lmermod2.propXP <- lmer(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                          prev_res_reb_lag3 + prev_res_gov_lag3 +
                          reb_X_d_lag2 + gov_X_d_lag2 +
                          physint + lngdppc + lntpop + rebstrength2 + 
                          coldwar +rebels_count + duration +
                          (1 | country) +(1 | year),
                        data = df)

lmermod2.propWP <- lmer(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                          prev_res_reb_lag3 + prev_res_gov_lag3 +
                          reb_W_d_lag2 + gov_W_d_lag2 +
                          physint + lngdppc + lntpop + rebstrength2 + 
                          coldwar +rebels_count + duration +
                          (1 | country) +(1 | year),
                        data = df)

lmermod2.propEP <- lmer(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                          prev_res_reb_lag3 + prev_res_gov_lag3 +
                          reb_E_d_lag2 + gov_E_d_lag2 +
                          physint + lngdppc + lntpop + rebstrength2 + 
                          (1 | country) +(1 | year),
                        data = df)

screenreg(list(mod2.prop, mod2.propXP, mod2.propWP, mod2.propEP,
                      lmermod2.prop, lmermod2.propXP,lmermod2.propWP,lmermod2.propEP),
          stars = c(0.01, 0.05, 0.1),digits = 3)

texreg(list(mod2.prop, mod2.propXP, mod2.propWP, mod2.propEP,
            lmermod2.prop, lmermod2.propXP,lmermod2.propWP,lmermod2.propEP), file = "Tables/Table_A1.tex",
       stars = c(0.01, 0.05, 0.1),
       caption = "Robustness Check: Alternative Regression Models for Attacks against Civilians in Civil Wars",
       caption.above = TRUE,
       label = "tab:model",
       use.packages = FALSE,
       scalebox = 0.7,
       booktabs = TRUE,
       custom.header = list("Fixed-effect models" = 1:4,
                            "Mixed-effect models" = 5:8),
       custom.coef.map = list("pro_reb_res_lag2" = "Pro-Rebel Resolution", 
                              "pro_gov_res_lag2" = "Pro-Government Resolution",
                              "prev_res_reb_lag3" = "Previous Pro Reb Res,Count", 
                              "prev_res_gov_lag3" = "Previous Pro Gov Res,Count", 
                              "any_reb_int_lag2"= "Any Pro-Rebel Support",
                              "any_gov_int_lag2"= "Any Pro-Government Support",
                              "reb_X_d_lag2" = "Pro Reb Intervention, Troops", 
                              "gov_X_d_lag2" = "Pro Gov Intervention, Troops", 
                              "reb_W_d_lag2" = "Pro Reb Intervention, Weapons",
                              "gov_W_d_lag2" = "Pro Gov Intervention, Weapons", 
                              "reb_E_d_lag2" = "Pro Reb Intervention, Economic", 
                              "gov_E_d_lag2" = "Pro Gov Intervention, Economic", 
                              "physint" = "Physical Integrity Rights", 
                              "lngdppc" = "Logged GDP per capita", 
                              "lntpop" = "Logged Population",
                              "rebstrength2" = "Rebel Strength",  
                              "coldwar" = "Cold War",
                              "rebels_count" = "Count of Rebel Groups",
                              "duration" = "Conflict Duration",
                              "(Intercept)" = "Intercept"),
       custom.gof.rows = list("Country Fixed/Mixed Effects" = c("YES", "YES", "YES", "YES", "YES", "YES","YES", "YES"),
                              "Year Fixed/Mixed Effects" = c("YES", "YES", "YES", "YES", "YES", "YES","YES", "YES")),
       digits = 3)

####Table A2####

#General Assembly
mod.prop.GA <- lmer(CivAttackProp ~ pro_reb_GA_lag2 + pro_gov_GA_lag2 + 
                      prev_reb_GA_lag3 + prev_gov_GA_lag3 + 
                      any_reb_int_lag2 + any_gov_int_lag2 +
                      physint + lngdppc + lntpop + rebstrength2 + 
                      coldwar +rebels_count + duration+
                      (1 | country) + (1 | year),
                    data = df)

mod.propX.GA <- lmer(CivAttackProp ~ pro_reb_GA_lag2 + pro_gov_GA_lag2 + 
                       prev_reb_GA_lag3 + prev_gov_GA_lag3 + 
                       reb_X_d_lag2 + gov_X_d_lag2 +
                       physint + lngdppc + lntpop + rebstrength2 + 
                       coldwar +rebels_count + duration+
                       (1 | country) + (1 | year),
                     data = df)

mod.propW.GA <- lmer(CivAttackProp ~ pro_reb_GA_lag2 + pro_gov_GA_lag2 + 
                       prev_reb_GA_lag3 + prev_gov_GA_lag3  +
                       reb_W_d_lag2 + gov_W_d_lag2 +
                       physint + lngdppc + lntpop + rebstrength2 + 
                       coldwar +rebels_count + duration+
                       (1 | country) + (1 | year),
                     data = df)

mod.propE.GA <- lmer(CivAttackProp ~ pro_reb_GA_lag2 + pro_gov_GA_lag2 + 
                       prev_reb_GA_lag3 + prev_gov_GA_lag3  +
                       reb_E_d_lag2 + gov_E_d_lag2 +
                       physint + lngdppc + lntpop + rebstrength2 + 
                       coldwar +rebels_count + duration+
                       (1 | country) + (1 | year),
                     data = df)

# Security Council 
mod.prop.SC <- lmer(CivAttackProp ~ pro_reb_SC_lag2 + pro_gov_SC_lag2 + 
                      prev_reb_SC_lag3 + prev_gov_SC_lag3 + 
                      any_reb_int_lag2 + any_gov_int_lag2 +
                      physint + lngdppc + lntpop + rebstrength2 + 
                      coldwar +rebels_count + duration+
                      (1 | country) + (1 | year),
                    data = df)

mod.propX.SC <- lmer(CivAttackProp ~ pro_reb_SC_lag2 + pro_gov_SC_lag2 + 
                       prev_reb_SC_lag3 + prev_gov_SC_lag3 + 
                       reb_X_d_lag2 + gov_X_d_lag2 +
                       physint + lngdppc + lntpop + rebstrength2 + 
                       coldwar +
                       (1 | country) + (1 | year),
                     data = df)

mod.propW.SC <- lmer(CivAttackProp ~ pro_reb_SC_lag2 + pro_gov_SC_lag2 + 
                       prev_reb_SC_lag3 + prev_gov_SC_lag3 + 
                       reb_W_d_lag2 + gov_W_d_lag2 +
                       physint + lngdppc + lntpop + rebstrength2 + 
                       coldwar +rebels_count + duration+
                       (1 | country) + (1 | year),
                     data = df)

mod.propE.SC <- lmer(CivAttackProp ~ pro_reb_SC_lag2 + pro_gov_SC_lag2 + 
                       prev_reb_SC_lag3 + prev_gov_SC_lag3 + 
                       reb_E_d_lag2 + gov_E_d_lag2 +
                       physint + lngdppc + lntpop + rebstrength2 + 
                       coldwar +rebels_count + duration+
                       (1 | country) + (1 | year),
                     data = df)

screenreg(list(mod.prop.GA, mod.propX.GA, mod.propW.GA, mod.propE.GA,
               mod.prop.SC, mod.propX.SC, mod.propW.SC,mod.propE.SC), stars = c(0.01, 0.05, 0.1),digits = 3)


texreg(list(mod.prop.GA, mod.propX.GA, mod.propW.GA, mod.propE.GA,
            mod.prop.SC, mod.propX.SC, mod.propW.SC,mod.propE.SC), file = "Tables/Table_A2.tex",
       stars = c(0.01, 0.05, 0.1),
       caption = "Robustness Check: The Impacts of General Assembly vs. Security Council Resolutions on Attacks against Civilians in Civil Wars",
       caption.above = TRUE,
       label = "tab:gasc",
       use.packages = FALSE,
       scalebox = 0.7,
       custom.header = list("General Assembly Resolutions" = 1:4,
                            "Security Council Resolutions" = 5:8),
       custom.coef.map = list("pro_reb_GA_lag2" = "Pro-Rebel Resolution", 
                              "pro_gov_GA_lag2" = "Pro-Government Resolution",
                              "pro_reb_SC_lag2" = "Pro-Rebel Resolution", 
                              "pro_gov_SC_lag2" = "Pro-Government Resolution",
                              "prev_reb_GA_lag3" = "Previous Pro Reb Res,Count", 
                              "prev_gov_GA_lag3" = "Previous Pro Gov Res,Count", 
                              "prev_reb_SC_lag3" = "Previous Pro Reb Res,Count", 
                              "prev_gov_SC_lag3" = "Previous Pro Gov Res,Count",
                              "any_reb_int_lag2"= "Any Pro-Rebel Support",
                              "any_gov_int_lag2"= "Any Pro-Government Support",
                              "reb_X_d_lag2" = "Pro Reb Intervention, Troops", 
                              "gov_X_d_lag2" = "Pro Gov Intervention, Troops", 
                              "reb_W_d_lag2" = "Pro Reb Intervention, Weapons",
                              "gov_W_d_lag2" = "Pro Gov Intervention, Weapons", 
                              "reb_E_d_lag2" = "Pro Reb Intervention, Economic", 
                              "gov_E_d_lag2" = "Pro Gov Intervention, Economic", 
                              "physint" = "Physical Integrity Rights", 
                              "lngdppc" = "Logged GDP per capita", 
                              "lntpop" = "Logged Population",
                              "rebstrength2" = "Rebel Strength",  
                              "coldwar" = "Cold War",
                              "rebels_count" = "Count of Rebel Groups",
                              "duration" = "Conflict Duration",
                              "(Intercept)" = "Intercept"),
       custom.gof.rows = list("Mixed Country Effects" = c("YES", "YES", "YES", "YES", "YES", "YES","YES", "YES"),
                              "Mixed Year Effects" = c("YES", "YES", "YES", "YES", "YES", "YES","YES", "YES")),
       digits = 3)

####Table A3####

#Unconditional models
l2.prop <- lm(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                prev_res_reb_lag3 + prev_res_gov_lag3 +
                any_reb_int_lag2 + any_gov_int_lag2 +
                physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag + ext_regime+ext_regime_gov+
                coldwar +rebels_count + duration,
              data = df)

l2.propXP <- lm(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                  prev_res_reb_lag3 + prev_res_gov_lag3 +
                  reb_X_d_lag2 + gov_X_d_lag2 +
                  physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag +   ext_regime+ext_regime_gov+
                  coldwar +rebels_count + duration,
                data = df)

l2.propWP <- lm(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                  prev_res_reb_lag3 + prev_res_gov_lag3 + 
                  reb_W_d_lag2 + gov_W_d_lag2 +
                  physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag +  ext_regime+ext_regime_gov+
                  coldwar +rebels_count + duration,
                data = df)

l2.propEP <- lm(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                  prev_res_reb_lag3 + prev_res_gov_lag3 + 
                  reb_E_d_lag2 + gov_E_d_lag2 +
                  physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag +  ext_regime+ext_regime_gov+
                  coldwar +rebels_count + duration,
                data = df)
summary(l2.propEP)

#Interaction models
l2.prop2 <- lm(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                 prev_res_reb_lag3 + prev_res_gov_lag3 +
                 any_reb_int_lag2*ext_regime + any_gov_int_lag2*ext_regime_gov +
                 physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag + ext_regime+ext_regime_gov+
                 coldwar +rebels_count + duration,
               data = df)

l2.propXP2 <- lm(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                   prev_res_reb_lag3 + prev_res_gov_lag3 +
                   reb_X_d_lag2*ext_regime + gov_X_d_lag2*ext_regime_gov +
                   physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag +   ext_regime+ext_regime_gov+
                   coldwar +rebels_count + duration,
                 data = df)

l2.propWP2 <- lm(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                   prev_res_reb_lag3 + prev_res_gov_lag3 + 
                   reb_W_d_lag2*ext_regime + gov_W_d_lag2*ext_regime_gov +
                   physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag +  ext_regime+ext_regime_gov+
                   coldwar +rebels_count + duration,
                 data = df)

l2.propEP2 <- lm(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                   prev_res_reb_lag3 + prev_res_gov_lag3 + 
                   reb_E_d_lag2*ext_regime + gov_E_d_lag2*ext_regime_gov +
                   physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag +  ext_regime+ext_regime_gov+
                   coldwar +rebels_count + duration,
                 data = df)


screenreg(list(l2.prop, l2.propXP, l2.propWP, l2.propEP,
               l2.prop2, l2.propXP2, l2.propWP2, l2.propEP2), stars = c(0.01, 0.05, 0.1),digits = 3)

texreg(list(l2.prop, l2.propXP, l2.propWP, l2.propEP,
            l2.prop2, l2.propXP2, l2.propWP2, l2.propEP2), file = "Tables/Table_A3.tex",
       stars = c(0.01, 0.05, 0.1),
       caption = "Robustness Check: Linear Regression Results for Attacks against Civilians in Civil Wars (Conditional on Regime Types of External State Supporters)",
       caption.above = TRUE,
       label = "tab:regime",
       use.packages = FALSE,
       booktabs = TRUE,
       scalebox = 0.6,
       custom.header = list("Unconditional models" = 1:4,
                            "Interaction models" = 5:8),
       custom.coef.map = list("CivAttackProp_lag" = "Lagged DV (Prop Attacks Against Civilians)",
                              "pro_reb_res_lag2" = "Pro-Rebel Resolution", 
                              "pro_gov_res_lag2" = "Pro-Government Resolution",
                              "any_reb_int_lag2"= "Any Pro-Rebel Support",
                              "any_gov_int_lag2"= "Any Pro-Government Support",
                              "reb_X_d_lag2" = "Pro Reb Intervention, Troops", 
                              "gov_X_d_lag2" = "Pro Gov Intervention, Troops", 
                              "reb_W_d_lag2" = "Pro Reb Intervention, Weapons",
                              "gov_W_d_lag2" = "Pro Gov Intervention, Weapons", 
                              "reb_E_d_lag2" = "Pro Reb Intervention, Economic", 
                              "gov_E_d_lag2" = "Pro Gov Intervention, Economic", 
                              "ext_regime_gov" = "Regime types of Pro Gov supporters",
                              "ext_regime" = "Regime types of Pro Reb supporters",
                              "any_reb_int_lag2:ext_regime" = "Any Pro-Rebel Support $\\times$ Regime types of Pro Reb supporters",
                              "any_gov_int_lag2:ext_regime_gov" =  "Any Pro-Government Support $\\times$ Regime types of Pro Gov supporters",
                              "reb_X_d_lag2:ext_regime" =  "Pro Reb Intervention, Troops $\\times$ Regime types of Pro Reb supporters",
                              "gov_X_d_lag2:ext_regime_gov" =  "Pro Gov Intervention, Troops $\\times$ Regime types of Pro Gov supporters",
                              "reb_W_d_lag2:ext_regime" =  "Pro Reb Intervention, Weapons $\\times$ Regime types of Pro Reb supporters",
                              "gov_W_d_lag2:ext_regime_gov" =  "Pro Gov Intervention, Weapons $\\times$ Regime types of Pro Gov supporters",
                              "reb_E_d_lag2:ext_regime" =  "Pro Reb Intervention, Economic $\\times$ Regime types of Pro Reb supporters",
                              "gov_E_d_lag2:ext_regime_gov" =  "Pro Gov Intervention, Economic $\\times$ Regime types of Pro Gov supporters",
                              "prev_res_reb_lag3" = "Previous Pro Reb Res,Count", 
                              "prev_res_gov_lag3" = "Previous Pro Gov Res,Count", 
                              "physint" = "Physical Integrity Rights", 
                              "lngdppc" = "Logged GDP per capita", 
                              "lntpop" = "Logged Population",
                              "rebstrength2" = "Rebel Strength",  
                              "coldwar" = "Cold War",
                              "rebels_count" = "Count of Rebel Groups",
                              "duration" = "Conflict Duration",
                              "(Intercept)" = "Intercept"),
       digits = 3)

####Figure A9####

p1_reb <- interplot(m = l2.propXP2, var1 = "reb_X_d_lag2", var2 = "ext_regime") +xlab("")+ 
  scale_x_continuous(expand  = c(0, 0.5), breaks = c(0, 1, 2), labels = c("No Support", "Pro-Reb Non-Democracies", "Pro-Reb Democracies"))+
  theme_bw()+ylab("Estimated Marginal Effects") +  geom_hline(aes(yintercept=0), linetype=2, color = "black")+
  theme(legend.position="bottom",
        legend.title=element_blank(),
        axis.text = element_text(size=10),
        text = element_text(size=10),
        plot.title = element_text(hjust = .5, size = 10, face = "bold")) +ggtitle("Pro Reb Intervention, Troops")

P1_gov <- interplot(m = l2.propXP2, var1 = "gov_X_d_lag2", var2 = "ext_regime_gov") +xlab("")+ 
  scale_x_continuous(expand  = c(0, 0.5), breaks = c(0, 1, 2), labels = c("No Support", "Pro-Gov Non-Democracies", "Pro-Gov Democracies"))+
  theme_bw()+ylab("Estimated Marginal Effects") +  geom_hline(aes(yintercept=0), linetype=2, color = "black")+
  theme(legend.position="bottom",
        legend.title=element_blank(),
        axis.text = element_text(size=10),
        text = element_text(size=10),
        plot.title = element_text(hjust = .5, size = 10, face = "bold")) +ggtitle("Pro Gov Intervention, Troops")


p2_reb<- interplot(m = l2.propWP2, var1 = "reb_W_d_lag2", var2 = "ext_regime") +xlab("")+ 
  scale_x_continuous(expand  = c(0, 0.5), breaks = c(0, 1, 2), labels = c("No Support", "Pro-Reb Non-Democracies", "Pro-Reb Democracies"))+
  theme_bw()+ylab("Estimated Marginal Effects") +  geom_hline(aes(yintercept=0), linetype=2, color = "black")+
  theme(legend.position="bottom",
        legend.title=element_blank(),
        axis.text = element_text(size=10),
        text = element_text(size=10),
        plot.title = element_text(hjust = .5, size = 10, face = "bold")) +ggtitle("Pro Reb Intervention, Weapons")


P2_gov <- interplot(m = l2.propWP2, var1 = "gov_W_d_lag2", var2 = "ext_regime_gov") +xlab("")+ 
  scale_x_continuous(expand  = c(0, 0.5), breaks = c(0, 1, 2), labels = c("No Support", "Pro-Gov Non-Democracies", "Pro-Gov Democracies"))+
  theme_bw()+ylab("Estimated Marginal Effects") +  geom_hline(aes(yintercept=0), linetype=2, color = "black")+
  theme(legend.position="bottom",
        legend.title=element_blank(),
        axis.text = element_text(size=10),
        text = element_text(size=10),
        plot.title = element_text(hjust = .5, size = 10, face = "bold")) +ggtitle("Pro Gov Intervention,  Weapons")


p3_reb<- interplot(m = l2.propEP2, var1 = "reb_E_d_lag2", var2 = "ext_regime") +xlab("")+ 
  scale_x_continuous(expand  = c(0, 0.5), breaks = c(0, 1, 2), labels = c("No Support", "Pro-Reb Non-Democracies", "Pro-Reb Democracies"))+
  theme_bw()+ylab("Estimated Marginal Effects") +  geom_hline(aes(yintercept=0), linetype=2, color = "black")+
  theme(legend.position="bottom",
        legend.title=element_blank(),
        axis.text = element_text(size=10),
        text = element_text(size=10),
        plot.title = element_text(hjust = .5, size = 10, face = "bold")) +ggtitle("Pro Reb Intervention, Economic")


P3_gov <- interplot(m = l2.propEP2, var1 = "gov_E_d_lag2", var2 = "ext_regime_gov") +xlab("")+ 
  scale_x_continuous(expand  = c(0, 0.5), breaks = c(0, 1, 2), labels = c("No Support", "Pro-Gov Non-Democracies", "Pro-Gov Democracies"))+
  theme_bw()+ylab("Estimated Marginal Effects") +  geom_hline(aes(yintercept=0), linetype=2, color = "black")+
  theme(legend.position="bottom",
        legend.title=element_blank(),
        axis.text = element_text(size=10),
        text = element_text(size=10),
        plot.title = element_text(hjust = .5, size = 10, face = "bold")) +ggtitle("Pro Gov Intervention, Economic")


regime_margin <- ggarrange(p1_reb, p2_reb,p3_reb,P1_gov, P2_gov,  P3_gov,
                           ncol = 2, nrow = 3, byrow = FALSE)
ggsave("Figures/Fig_A9.jpeg", 
       plot = regime_margin, dpi = 600, width = 12, height = 8 )

####Table A4####

df_drop <- df %>%
  dplyr::group_by(conflict_id) %>%
  dplyr::arrange(conflict_id,month) %>% 
  dplyr::select(conflict_id,month, pro_reb_res, pro_reb_res_lag, pro_reb_res_lag2, prev_res_reb_lag2, prev_res_reb_lag3, everything())%>%
  dplyr::filter(prev_res_reb_lag3 < 2)

l2.propXP <- lm(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                  prev_res_reb_lag3 + prev_res_gov_lag3 + 
                  reb_X_d_lag2 + gov_X_d_lag2 +
                  physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag + 
                  coldwar +rebels_count + duration,
                data = df_drop)

l2.propWP <- lm(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                  prev_res_reb_lag3 + prev_res_gov_lag3 +
                  reb_W_d_lag2 + gov_W_d_lag2 +
                  physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag +
                  coldwar +rebels_count + duration,
                data = df_drop)

l2.propEP <- lm(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                  prev_res_reb_lag3 + prev_res_gov_lag3 +
                  reb_E_d_lag2 + gov_E_d_lag2 +
                  physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag +
                  coldwar +rebels_count + duration,
                data = df_drop)

l2.propXP2 <- lm(CivAttackProp ~ prev_res_reb_lag3 + prev_res_gov_lag3 + 
                   reb_X_d_lag2 + gov_X_d_lag2 +
                   physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag + 
                   coldwar +rebels_count + duration,
                 data = df_drop)

l2.propWP2 <- lm(CivAttackProp ~  prev_res_reb_lag3 + prev_res_gov_lag3 +
                   reb_W_d_lag2 + gov_W_d_lag2 +
                   physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag +
                   coldwar +rebels_count + duration,
                 data = df_drop)

l2.propEP2 <- lm(CivAttackProp ~  prev_res_reb_lag3 + prev_res_gov_lag3 +
                   reb_E_d_lag2 + gov_E_d_lag2 +
                   physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag +
                   coldwar +rebels_count + duration,
                 data = df_drop)


screenreg(list(l2.propXP, l2.propXP2, l2.propWP, l2.propWP2, l2.propEP, l2.propEP2), stars = c(0.01, 0.05, 0.1),digits = 3)


texreg(list(l2.propXP, l2.propXP2, l2.propWP, l2.propWP2, l2.propEP, l2.propEP2), file = "Tables/Table_A4.tex",
       stars = c(0.01, 0.05, 0.1),
       caption = "Robustness Check: Linear Regression Results for Attacks against Civilians in Civil Wars (Excluding conflicts after the first resolution)",
       caption.above = TRUE,
       label = "tab:drop",
       use.packages = FALSE,
       booktabs = TRUE,
       scalebox = 0.6,
       custom.header = list("Troops Intervention" = 1:2,
                            "Weapons Intervention" = 3:4,
                            "Economic Intervention" = 5:6),
       custom.model.names = c("Model 1", "Model 2", "Model 3", "Model 4", "Model 5", "Model 6"),
       custom.coef.map = list("CivAttackProp_lag" = "Lagged DV (Prop Attacks Against Civilians)",
                              "pro_reb_res_lag2" = "Pro-Rebel Resolution", 
                              "pro_gov_res_lag2" = "Pro-Government Resolution",
                              "prev_res_reb_lag3" = "Previous Pro Reb Res,Count", 
                              "prev_res_gov_lag3" = "Previous Pro Gov Res,Count", 
                              "any_reb_int_lag2"= "Any Pro-Rebel Support",
                              "any_gov_int_lag2"= "Any Pro-Government Support",
                              "reb_X_d_lag2" = "Pro Reb Intervention, Troops", 
                              "gov_X_d_lag2" = "Pro Gov Intervention, Troops", 
                              "reb_W_d_lag2" = "Pro Reb Intervention, Weapons",
                              "gov_W_d_lag2" = "Pro Gov Intervention, Weapons", 
                              "reb_E_d_lag2" = "Pro Reb Intervention, Economic", 
                              "gov_E_d_lag2" = "Pro Gov Intervention, Economic", 
                              "physint" = "Physical Integrity Rights", 
                              "lngdppc" = "Logged GDP per capita", 
                              "lntpop" = "Logged Population",
                              "rebstrength2" = "Rebel Strength",  
                              "coldwar" = "Cold War",
                              "rebels_count" = "Count of Rebel Groups",
                              "duration" = "Conflict Duration",
                              "(Intercept)" = "Intercept"),
       digits = 3)

####Table A5####

df2 <- df %>% dplyr::select(conflict_id,month, cnty_month, year, country,
                            CivAttackProp, pro_reb_res_lag2,pro_gov_res_lag2 ,
                            prev_res_reb_lag3 , prev_res_gov_lag3 ,
                            any_reb_int_lag2 , any_gov_int_lag2 ,
                            physint ,lngdppc ,lntpop , rebstrength2 ,
                            reb_X_d_lag2 , gov_X_d_lag2 ,
                            coldwar,rebels_count , duration,gwno_a,
                            reb_W_d_lag2 , gov_W_d_lag2 ,
                            reb_E_d_lag2 , gov_E_d_lag2 ,CivAttackProp_lag)
df2 <- na.omit(df2)

m.out1 <- matchit(pro_reb_res_lag2 ~ physint + lngdppc + lntpop + rebels_count, data = df2,
                  method = "cem")
summary(m.out1)
m_data <- match.data(m.out1)

mod2.prop <- lm(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                  prev_res_reb_lag3 + prev_res_gov_lag3 +
                  any_reb_int_lag2 + any_gov_int_lag2 +
                  physint + lngdppc + lntpop + rebstrength2 + 
                  coldwar +rebels_count + duration+
                  CivAttackProp_lag,
                data = m_data, weights = weights)

mod2.propXP <- lm(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                    prev_res_reb_lag3 + prev_res_gov_lag3 +
                    reb_X_d_lag2 + gov_X_d_lag2 +
                    physint + lngdppc + lntpop + rebstrength2 + 
                    coldwar +rebels_count + duration +
                    CivAttackProp_lag,
                  data = m_data, weights = weights)

mod2.propWP <- lm(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                    prev_res_reb_lag3 + prev_res_gov_lag3 +
                    reb_W_d_lag2 + gov_W_d_lag2 +
                    physint + lngdppc + lntpop + rebstrength2 + 
                    coldwar +rebels_count + duration +
                    CivAttackProp_lag,
                  data = m_data, weights = weights)

mod2.propEP <- lm(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                    prev_res_reb_lag3 + prev_res_gov_lag3 +
                    reb_E_d_lag2 + gov_E_d_lag2 +
                    physint + lngdppc + lntpop + rebstrength2 + 
                    coldwar +rebels_count + duration +
                    CivAttackProp_lag,
                  data = m_data, weights = weights)
screenreg(list(mod2.prop, mod2.propXP, mod2.propWP,mod2.propEP), stars = c(0.01, 0.05, 0.1), digits = 3)

texreg(list(mod2.prop, mod2.propXP, mod2.propWP,mod2.propEP), file = "Tables/Table_A5.tex",
       stars = c(0.01, 0.05, 0.1),
       caption = "Robustness Check: Linear Regression Results for Attacks against Civilians in Civil Wars (Matched Sample)",
       caption.above = TRUE,
       label = "tab:match",
       use.packages = FALSE,
       booktabs = TRUE,
       scalebox = 0.8,
       custom.header = list("Any Material Support" = 1,
                            "Disaggregated Material Support" = 2:4),
       custom.model.names = c("Model 1", "Model 2", "Model 3", "Model 4"),
       custom.coef.map = list("CivAttackProp_lag" = "Lagged DV (Prop Attacks Against Civilians)",
                              "pro_reb_res_lag2" = "Pro-Rebel Resolution", 
                              "pro_gov_res_lag2" = "Pro-Government Resolution",
                              "prev_res_reb_lag3" = "Previous Pro Reb Res,Count", 
                              "prev_res_gov_lag3" = "Previous Pro Gov Res,Count", 
                              "any_reb_int_lag2"= "Any Pro-Rebel Support",
                              "any_gov_int_lag2"= "Any Pro-Government Support",
                              "reb_X_d_lag2" = "Pro Reb Intervention, Troops", 
                              "gov_X_d_lag2" = "Pro Gov Intervention, Troops", 
                              "reb_W_d_lag2" = "Pro Reb Intervention, Weapons",
                              "gov_W_d_lag2" = "Pro Gov Intervention, Weapons", 
                              "reb_E_d_lag2" = "Pro Reb Intervention, Economic", 
                              "gov_E_d_lag2" = "Pro Gov Intervention, Economic", 
                              "physint" = "Physical Integrity Rights", 
                              "lngdppc" = "Logged GDP per capita", 
                              "lntpop" = "Logged Population",
                              "rebstrength2" = "Rebel Strength",  
                              "coldwar" = "Cold War",
                              "rebels_count" = "Count of Rebel Groups",
                              "duration" = "Conflict Duration",
                              "(Intercept)" = "Intercept"),
       digits = 3)


